<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class CommShare extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_order_commission_share';




    /**
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @param $uniacid
     * @param $balance
     * @param $cash
     * @param $id
     * @param $type
     * @param int $share_id
     * @功能说明:
     */
    public function addData($uniacid,$balance,$cash,$id,$type,$order_id,$share_id=0){

        $insert = [

            'uniacid' => $uniacid,

            'comm_id' => $id,

            'share_balance' => $balance,

            'share_cash' => $cash,

            'type' => $type,

            'share_id' => $share_id,

            'order_id' => $order_id,
        ];

        $res = $this->insert($insert);

        return $res;
    }


    /**
     * @功能说明:获取分摊比例
     */
    public function getShareBalance($comm_id,$type){

        $dis = [

            'comm_id' => $comm_id,

            'type'    => $type
        ];

        $balance = $this->where($dis)->value('share_balance');

        return !empty($balance)?$balance:0;

    }


    /**
     * @功能说明:添加分摊记录
     */
    public function addPointData($id,$order,$share_type=0){

        $comm_model = new Commission();

        $type   = $comm_model->where(['id'=>$id])->value('type');

        $top_id = $comm_model->where(['id'=>$id])->value('top_id');

        $cash   = $comm_model->getTypeText($type);

        if(!empty($cash['point'])&&!empty($order['pay_point'])){

            $insert = [

                'uniacid'      => $order['uniacid'],

                'comm_id'      => $id,

                'share_balance'=> $order['pay_point'],

                'share_cash'   => $order[$cash['point']],

                'order_id'     => $order['id'],

                'cash_type'    => 1,

                'share_id'     => $top_id,

                'comm_type'    => $type
            ];

            $this->dataAdd($insert);
        }

        return true;
    }


    /**
     * @功能说明:添加广告费记录
     */
    public function addPosterData($id,$order,$type=1){

        if(!empty($order['poster_cash'])){

            if($type==1){

                $balance = $order['poster_coach_share'];

                $cash    = $order['share_poster_coach'];

            }elseif ($type==2){

                $balance = $order['poster_admin_share'];

                $cash    = $order['share_poster_admin'];

            }else{

                $balance = $order['poster_company_share'];

                $cash    = $order['share_poster_company'];
            }

            $comm_model = new Commission();

            $top_id = $comm_model->where(['id'=>$id])->value('top_id');

            $insert = [

                'uniacid' => $order['uniacid'],

                'comm_id' => $id,

                'share_balance'=> $balance,

                'share_cash'   => $cash,

                'order_id'     => $order['id'],

                'cash_type'    => 2,

                'share_id'     => $top_id,

                'type'   => $type
            ];

            $res = $this->dataAdd($insert);

            return $res;
        }

        return true;

    }


    /**
     * @功能说明:技师储值
     */
    public function coachBalanceCash($id,$order){

        if(isset($order['comm_coach_balance'])&&$order['comm_coach_balance']>0){

            $comm_model = new Commission();

            $top_id = $comm_model->where(['id'=>$id])->value('top_id');

            $insert = [

                'uniacid' => $order['uniacid'],

                'comm_id' => $id,

                'share_balance'=> $order['comm_coach_balance'],

                'share_cash'   => $order['comm_coach_cash'],

                'order_id'     => $order['id'],

                'cash_type'    => 3,

                'share_id'     => $top_id,

                'type'         => 1,

                'comm_type'    => 3,
            ];

            $res = $this->dataAdd($insert);

            return $res;
        }

        return true;

    }


    /**
     * @功能说明:技师储值
     */
    public function coachSkillCash($id,$order){

        if(isset($order['skill_balance'])&&$order['skill_balance']>0&&!empty($order['coach_id'])){

            $comm_model = new Commission();

            $top_id = $comm_model->where(['id'=>$id])->value('top_id');

            $insert = [

                'uniacid' => $order['uniacid'],

                'comm_id' => $id,

                'share_balance'=> $order['skill_balance'],

                'share_cash'   => $order['skill_cash'],

                'order_id'     => $order['id'],

                'cash_type'    => 4,

                'share_id'     => $top_id,

                'type'         => 1,

                'comm_type'    => 3,
            ];

            $res = $this->dataAdd($insert);

            return $res;
        }

        return true;

    }

    /**
     * @功能说明:获取手续费
     */
    public function getOrderPointCash($order_id,$type=1){

        if($type==1){

            $cash = $this->where('order_id','in',$order_id)->where(['cash_type'=>1])->sum('share_cash');
        }else{

            $cash = $this->where('order_id','=',$order_id)->where(['cash_type'=>1])->sum('share_cash');
        }

        return round($cash,2);
    }


    /**
     * @param $coach_id
     * @功能说明:技师佣金手续费
     */
    public function getCoachPointCash($coach_id){

        $dis[] = ['b.top_id','=',$coach_id];

        $dis[] = ['b.status','=',2];
        //车费和服务费
        $dis[] = ['b.type','in',[3,8]];

        $dis[] = ['a.cash_type','=',1];

        $cash = $this->alias('a')
                ->join('massage_service_order_commission b','a.comm_id = b.id')
                ->where($dis)
                ->group('a.id')
                ->sum('a.share_cash');

        return $cash;
    }


    /**
     * @功能说明:技师分摊了业务员|渠道上|合伙额多少钱
     */
    public function coachShareCash($coach_id,$type,$order_id=0){

        $dis[] = ['a.share_id','=',$coach_id];

        $dis[] = ['b.status','=',2];

        $dis[] = ['b.type','=',$type];

        $dis[] = ['a.cash_type','=',0];

        $dis[] = ['a.type','=',1];

        if(!empty($order_id)){

            $dis[] = ['b.order_id','=',$order_id];
        }

        $cash = $this->alias('a')
            ->join('massage_service_order_commission b','a.comm_id = b.id')
            ->where($dis)
            ->group('a.id')
            ->sum('a.share_cash');

        return round($cash,2);
    }


    /**
     * @param $coach_id
     * @param $type
     * @功能说明:技师广告费
     */
    public function coachPosterCash($coach_id,$type){

        $dis[] = ['a.share_id','=',$coach_id];

        $dis[] = ['b.status','=',2];

        $dis[] = ['a.type','=',1];

        $dis[] = ['a.cash_type','=',$type];

        $cash = $this->alias('a')
            ->join('massage_service_order_commission b','a.comm_id = b.id')
            ->where($dis)
            ->group('a.id')
            ->sum('a.share_cash');

        return $cash;
    }


    /**
     * @param $coach_id
     * @param int $order_id
     * @功能说明:技师分摊手续费
     */
    public function coachShareCashData($coach_id,$order_id=0){
        //获取分摊金额
        $arr = [
            //合伙人分摊
            9 => 'partner_share_cash',
            //渠道商
            10=> 'channel_share_cash',
            //业务员
            12=> 'salesman_share_cash',

        ];
        //总分摊
        $order['total_share_cash'] = 0;

        foreach ($arr as $k=>$v){

            $order[$v] = $this->coachShareCash($coach_id,$k,$order_id);

            $order['total_share_cash'] += $order[$v];
        }

        $where = [];

        if(!empty($order_id)){

            $where = [

                'order_id' => $order_id
            ];
        }
        //手续费(只查服务费)
        $order['point_cash']  = $this->where($where)->where(['cash_type'=>1,'comm_type'=>3])->sum('share_cash');
        //广告费
        $order['poster_cash'] = $this->where($where)->where(['cash_type'=>2,'type'=>1])->sum('share_cash');
        //储值扣款
        $order['coach_balance_cash'] = $this->where($where)->where(['cash_type'=>3,'type'=>1])->sum('share_cash');

        $order['total_share_cash'] = round($order['total_share_cash']+$order['point_cash']+ $order['poster_cash']+$order['coach_balance_cash'],2);

        return $order;
    }





}